package uk.ac.open.kmi.dyniqx.repositories;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.lucene.document.Field;
import org.directwebremoting.WebContext;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;

import uk.ac.open.kmi.dyniqx.storages.Storage;

public class IntuteThread extends Thread {
	
	public String queryString;
	public int count;

	public Storage storage;
	public WebContext wctx;
	
	public volatile boolean stop = false;
	
	public Logger logger = Logger.getLogger("uk.ac.open.kmi.dyniqx.repositories.IntuteThread");
	
	public IntuteThread(Storage storage, WebContext wctx, String queryString, int count) {
		
		this.setStorage(storage);
		this.setWctx(wctx);
		this.setQueryString(queryString);
		this.setCount(count);
		
	}
	
	@SuppressWarnings("unchecked")
	public void run() {
		
		this.logger.debug("Thread started.");
		
		SAXBuilder parser = new SAXBuilder();
		String searchUrl = "http://www.intute.ac.uk/include2/cgi-bin/include_search2.pl?q=" + this.getQueryString();
		if (this.count > 100) this.count = 100;
		int startIndex = 1;
		int max = 20;
		int temp_count = 0;
		Document doc;
		
		/* Open University proxy Settings */

		System.getProperties().put( "proxySet", "true" );
		System.getProperties().put( "proxyHost", "wwwcache" );
		System.getProperties().put( "proxyPort", "80" );
		System.setProperty("java.net.useSystemProxies","true");
		
		try {
			
			while ((startIndex <= this.count) && !stop) {
				
				temp_count = 0;
				doc = parser.build(searchUrl + "&count=" + max + "&startIndex=" + startIndex);
	
				List<Element> list = (List<Element>) XPath.newInstance("/rss/channel/item").selectNodes(doc);
			    Iterator iterator = list.iterator();
			    
			    org.apache.lucene.document.Document result = null;
			    ArrayList<org.apache.lucene.document.Document> documentArray = new ArrayList<org.apache.lucene.document.Document>();
			    
			    while (iterator.hasNext()) {
			        Element el = (Element) iterator.next();
			        
			        String id = XPath.newInstance("link").valueOf(el);
			        String title = XPath.newInstance("title").valueOf(el);
			        String description = XPath.newInstance("description").valueOf(el);
			        String link = XPath.newInstance("link").valueOf(el);		        
			        String internal = XPath.newInstance("intute:redirectLink").valueOf(el);
			        
			        result = new org.apache.lucene.document.Document();

			        result.add(new Field("source", "Int", Field.Store.YES, Field.Index.UN_TOKENIZED));
			        result.add(new Field("type", "text", Field.Store.YES, Field.Index.UN_TOKENIZED));
			        result.add(new Field("id", id, Field.Store.YES, Field.Index.UN_TOKENIZED));
			        result.add(new Field("query", queryString, Field.Store.YES, Field.Index.UN_TOKENIZED));
			        result.add(new Field("rank", Integer.toString(++temp_count), Field.Store.YES, Field.Index.UN_TOKENIZED));
			        
			        result.add(new Field("link", link, Field.Store.YES, Field.Index.UN_TOKENIZED));
			        result.add(new Field("internal", internal, Field.Store.YES, Field.Index.UN_TOKENIZED));
			        result.add(new Field("title", title, Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.YES));
			        result.add(new Field("description", description, Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.YES));
			        
			        result.add(new Field("date", "n/a", Field.Store.YES, Field.Index.UN_TOKENIZED));
			        result.add(new Field("authors", "n/a", Field.Store.YES, Field.Index.TOKENIZED));
			        result.add(new Field("journal", "n/a", Field.Store.YES, Field.Index.UN_TOKENIZED));
			        
			        result.add(new Field("summary", "n/a", Field.Store.YES, Field.Index.UN_TOKENIZED));
			        result.add(new Field("width", "n/a", Field.Store.YES, Field.Index.UN_TOKENIZED));
			        result.add(new Field("height", "n/a", Field.Store.YES, Field.Index.UN_TOKENIZED));
			        result.add(new Field("fileSize", "n/a", Field.Store.YES, Field.Index.UN_TOKENIZED));
			        result.add(new Field("fileFormat", "n/a", Field.Store.YES, Field.Index.UN_TOKENIZED));
			        result.add(new Field("thumbUrl", "n/a", Field.Store.YES, Field.Index.UN_TOKENIZED));
			        result.add(new Field("thumbWidth", "n/a", Field.Store.YES, Field.Index.UN_TOKENIZED));
			        result.add(new Field("thumbHeight", "n/a", Field.Store.YES, Field.Index.UN_TOKENIZED));
			        			        
			        result.setBoost(100 / temp_count);
			        
			        documentArray.add(result);
			    }
			    
			    this.storage.performIOAction(Storage.WRITE_ACTION, documentArray);
			    this.storage.updateGrid();

			    startIndex += max;
			}
		    
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}		
		
		if (this.stop) {
			this.logger.debug("Thread killed.");
            return;
        }
		
		this.logger.debug("Thread ended.");
    }
	
	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}
	
	public String getQueryString() {
		return this.queryString;
	}
	
	public void setCount(int count) {
		this.count = count;
	}
	
	public int getCount() {
		return this.count;
	}
	
	public void setStorage(Storage storage) {
		this.storage = storage;
	}	
	
	public Storage getStorage() {
		return this.storage;
	}

	public WebContext getWctx() {
		return wctx;
	}

	public void setWctx(WebContext wctx) {
		this.wctx = wctx;
	}
}
